const CosmeticsClassifyModule = require('../../modules/base/cosmeticsClassify');
const ClassifyFieldMap = require('../../modules/map/classifyFieldMap');
const qs = require('querystring');


class CosmeticsClassifyControllers {
  /**
   * 新增化妆品分类
   */
  static async addCosmeticsClassify(ctx) {
    const req = ctx.request.body
    if (!req.classify || !req.content) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    } else {
      const exist = (await CosmeticsClassifyModule.getClassify({
        cosmeticsClassify: req.classify
      })).length
      if (exist) {
        ctx.body = {
          msg: '该分类已存在'
        }
        return
      }
      try {
        await CosmeticsClassifyModule.addClassify({ classify: req.classify, content: req.content })
        ctx.body = {
          code: 200,
          msg: 'success'
        }
      } catch(err) {
        console.log(err)
        ctx.body = {
          code: 500,
          msg: err.stack
        }
      }
    }
  }

  /**
   * 更新分类item
   */
  static async updateClassifyContent(ctx) {
    const req = ctx.request.body
    if (!req.content || !req.value) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    } else {
      try {
        let exist = await ClassifyFieldMap.getMap({value: req.value})
        if (exist.length) {
          ctx.body = {
            code: 416,
            msg: '该分类已存在'
          }
          return
        }
        let list = await ClassifyFieldMap.getMap({content: req.content})
        let num
        if (list.length === 0) {
          num = 0
        } else {
          num = +(list[list.length - 1].key.split('_')[1]) + 1
        }
        await ClassifyFieldMap.setMap(`${req.content}_${num}`, req.value, req.content)
        ctx.body = {
          code: 200,
          msg: 'success'
        }
      } catch (err) {
        console.log(err)
        ctx.body = {
          code: 400,
          msg: err.stack
        }
      }
    }
  }

  /**
   * 查询分类列表
   */
  static async getClassifyList(ctx) {
    const list = await CosmeticsClassifyModule.getClassify({})
    let data = []
    for(var i = 0; i < list.length; i++) {
      await (function(){
        return new Promise(function(res, rej){
          (async () => {
            let l = await ClassifyFieldMap.getMap({content: list[i].content})
            data.push({
              classify: list[i].cosmeticsClassify,
              content: list[i].content,
              list: l
            })
            res()
          })()
        })
      }())
    }
    ctx.body = {
      code: 200,
      data
    }
  }

  /**
   * 删除分类 
   */
  static async deleteClassifyList(ctx) {
    const req = ctx.request.body
    if (!req.classify || !req.content) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    } else {
      try {
        await CosmeticsClassifyModule.deleteClassify({
          cosmeticsClassify: req.classify
        })
        await ClassifyFieldMap.deleteMap({
          content: req.content
        })
        ctx.body = {
          code: 200,
          msg: 'success'
        }
      } catch (err) {
        console.log(err)
        ctx.body = {
          code: 400,
          msg: err.stack
        }
      }
    }
  }

  /**
   * 删除分类item
   */
  static async deleteClassifyMap(ctx) {
    const req = ctx.request.body
    if (!req.content || !req.value) {
      ctx.body = {
        code: 416,
        msg: '缺少参数'
      }
      return
    } else {
      try {
        await ClassifyFieldMap.deleteMap({
          content: req.content,
          value: req.value
        })
        ctx.body = {
          code: 200,
          msg: 'success'
        }
      } catch (err) {
        console.log(err)
        ctx.body = {
          code: 400,
          msg: err.stack
        }
      }
    }
  }

  /**
   * 查询分类map
   */
  static async getClassifyMap(ctx) {
    const req = qs.parse(ctx.request.url.split('?')[1])
    // if (!req.content) {
    //   ctx.body = {
    //     code: 416,
    //     msg: '缺少参数'
    //   }
    //   return
    // }
    let where = {}
    if (req.content) {
      where = {
        content: req.content
      }
    }
    try {
      let list = await ClassifyFieldMap.getMap(where)
      ctx.body = {
        code: 200,
        data: list
      }
    } catch (err) {
      console.log(err)
      ctx.body = {
        code: 400,
        msg: err.stack
      }
    }
  }
}

module.exports = CosmeticsClassifyControllers